#include "payment.h"

float credit;
//交易
void pay(void)
{
	char reid[10];
	int count=0;
	printf("请输入货品编号：");
	get_str(reid,sizeof(reid));
	int index=(query_goods(reid));
	if(0>index)
	{
		print_sec("输入错误！",0.75);
		return;
	}
	printf("请输入购买个数：");
	scanf("%d",&count);
	if(count > goods[index].num)
	{
		print_sec("购买个数超过商品库存！",0.75);
	}
	else if(count<=0)
	{
		print_sec("商品数量输入有误！",0.75);
	}
	else
	{
		goods[index].num-=count;
		payment[cnt_payment].crediting = (goods[index].sell_price) * count;
		payment[cnt_payment].num=count;
		strcpy(payment[cnt_payment].goods_name,goods[index].goods_name);
		cnt_payment++;
	}																	
}

//交易界面
void menu_pay(void)
{
	credit=0;
	for(;;)
	{
		system("clear");
		puts("****是否继续交易****");
		puts("1、继续");
		puts("0、返回");
		switch(get_cmd('0','1'))
		{
			case '1':pay();break;
			case '0':return;
		}
	}
}

//普通用户
void common(void)
{
	char phone[12];
	menu_pay();
	printf("您需向%s付款%.2f元\n",administrator[cnt_administrator].phone,credit);
	printf("请输入扣款账户！");
	get_phone(phone,sizeof(phone));
	strcpy(payment[cnt_payment].phone,phone);
	print_sec("购买成功！",0.75);
}

//vip用户
void vip_(void)
{
	// 输入用户名
	char rename[20];
	printf("请输入用户名：");
	get_str(rename,sizeof(rename));
	int index = query_vip(rename);
	if(0 > index)
	{
		print_sec("您不是本店vip！",0.75);
		return;
	}
	menu_pay();
	vip[index].points+=payment[cnt_payment-1].crediting;
	strcpy(payment[cnt_payment-1].phone,vip[index].phone);
	printf("您需向%s付款%.2f元\n",administrator[cnt_administrator-1].phone,credit);
	print_sec("购买成功！",0.75);
}

//收银
void cashier(void)
{
	for(;;)
	{
		system("clear");
		puts("****收银****");
		puts("1、普通用户");
		puts("2、vip用户");
		puts("0、返回");
		switch(get_cmd('0','9'))
		{
			case '1':common();break;
			case '2':vip_();break;
			case '0':return;
		}
	}
}
